package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"yangming-college/app/usercenter/cmd/v1/model"
	"yangming-college/app/usercenter/cmd/v1/rpc/internal/svc"
	"yangming-college/app/usercenter/cmd/v1/rpc/pb"
	"yangming-college/app/usercenter/cmd/v1/rpc/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserDetailLogic {
	return &GetUserDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserDetailLogic) GetUserDetail(in *pb.UserInfoRequest) (*pb.UserInfoResponse, error) {
	var account model.Account
	err := l.svcCtx.DB.Table("accounts").Where("account_id = ?", in.Id).Find(&account).Error
	if err != nil {
		return nil, errors.Wrapf(err, "查找用户失败")
	}
	var respAccount usercenter.Account
	copier.Copy(&respAccount, account)
	return &pb.UserInfoResponse{
		Account: &respAccount,
	}, nil
}
